import { defineStore } from 'pinia'
import { reactive } from 'vue'
import type { IUser } from '@/global/type'
import { localCache } from '@/utils/storage.ts'
import {
  getUserInfoApi,
  getuserTokenApi
} from '@/servers/login/UserLoginInfo.ts'
import type { IUserInfo } from '@/global/type'
import type { IRouter } from '@/global/type/menu'
interface Itoken {
  token: string
}
const useUser = defineStore('user', () => {
  let user = reactive({
    name: '',
    avatar: '',
    token: localCache.getCache('token') ?? '',
    menu: [] as IRouter[]
  })
  const getterUserTokenAysnc = async (userInfo: IUser) => {
    const res = await getuserTokenApi<Itoken>(userInfo)
    localCache.setCache('token', res.token)
    const menuInfo = await getterUserInfoAysnc(res.token)
    return menuInfo
  }
  const getterUserInfoAysnc = async (token: string) => {
    const res = await getUserInfoApi<IUserInfo>(token)
    return res
  }
  return {
    user,
    getterUserTokenAysnc,
    getterUserInfoAysnc
  }
})

export default useUser
